package org.fuys.coder.domain.auth.service.holder;

import org.fuys.coder.infrastructure.model.dto.UserPermissionDTO;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.holder
 * @className: PermissionHolder
 * @author: WangJing
 * @description: 用户Id拥有的权限 由SpringBoot启动钩子注入
 * @date: 2024/7/10 23:55
 * @version: 1.0
 */
public class PermissionHolder {
    private final static Map<Integer, UserPermissionDTO> userIdPermissionMap=new ConcurrentHashMap<>();

    public static UserPermissionDTO getPermission(Integer userId){
        return userIdPermissionMap.get(userId);
    }

    public static void setPermission(Integer userId,UserPermissionDTO permissionDTO){
        userIdPermissionMap.put(userId,permissionDTO);
    }

    public static void initPermission(List<UserPermissionDTO> commonRoleDetail){
        commonRoleDetail.forEach((item)->{
            userIdPermissionMap.put(item.getUserId(),item);
        });
    }

    public static boolean hasUser(Integer userId){
        return userIdPermissionMap.containsKey(userId);
    }
}
